<template>
  <div class='container-subjects'>
    <el-card>
      <!--筛选 -->
      <el-row>
        <!-- 表单 -->
        <el-col :span="18">
          <el-form label-width="80px" inline size="small">
            <el-form-item label="学科名称">
              <el-input v-model="reqParams.subjectName" placeholder="搜索关键字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 按钮 -->
        <el-button
        size="small"
        type="success"
        icon="el-icon-edit"
        style="float:right"
        @click="addSubjects">
        添加学科
        </el-button>
        <el-col :span="6"></el-col>
        </el-row>

      <!--总计 -->
       <el-alert
    :title="`数据一共${total}条`"
    type="info"
    show-icon
    :closable="false"
    style="margin-bottom:15px"
    >
  </el-alert>
      <!--表格 -->
      <el-table :data="subjects">
         <el-table-column type="index" label="序号"></el-table-column>
         <el-table-column prop="subjectName" label="学科名称" ></el-table-column>
         <el-table-column prop="username" label="创建者"></el-table-column>
         <el-table-column label="创建日期" width="160px">
           <template slot-scope="scope">
             {{scope.row.addDate | parseTimeByString}}
           </template>
         </el-table-column>
         <el-table-column label="前台是否显示">
            <template slot-scope="scope">
             {{scope.row.isFrontDisplay === 1 ? '是' : '否'}}
           </template>
         </el-table-column>
         <el-table-column prop="twoLevelDirectory" label="二级目录"></el-table-column>
         <el-table-column prop="tags" label="标签"></el-table-column>
         <el-table-column prop="totals" label="题目数量"></el-table-column>
         <el-table-column label="操作" width="240px">
            <template slot-scope="scope" >
             <!-- <el-link @click="$router.push({path:'directorys',query:{id:scope}})" type="primary" :underline="false">学科分类</el-link> -->
             <el-link @click="toDiretory(scope.row)" type="primary" :underline="false">学科分类</el-link>
             <el-link @click="toTag(scope.row)" type="primary" :underline="false">学科标签</el-link>
             <el-link @click="editSubject(scope.row)" type="primary" :underline="false">修改</el-link>
             <el-link @click="delSubject(scope.row.id)" type="primary" :underline="false">删除</el-link>
           </template>
         </el-table-column>
      </el-table>
      <!--分页监听页面尺寸改变 size-change -->
       <el-pagination
       class="pager"
        background
        layout="prev, pager, next,sizes,jumper"
        :total="total"
        :current-page="reqParams.page"
        :page-size="reqParams.pagesize"
        @current-change="changePage"
        @size-change="changeSize"
        :page-sizes="[5,10,20,50]"
      />
    </el-card>
    <!-- 弹层 -->
      <subjects-add
      ref="subjectsAdd"
      @getSubjects="getSubjects()"
      :show-dialog.sync="showDialog"
      :perForm="currSubject"
      ></subjects-add>
  </div>
</template>

<script>
import { list as subjectsList, remove as subjectsDel } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'subjects-page',
  components: { SubjectsAdd },
  data () {
    return {
      // 筛选参数
      reqParams: {
        // 学科名字,关键字
        subjectName: null,
        // 页码
        page: 1,
        // 尺寸
        pagesize: 10
      },
      // 学科列表
      subjects: [],
      // 总条数
      total: 0,
      // 当前的subject数据是{} 就是添加 数据是{id,subjectName,...} 就是修改
      currSubject: {},
      showDialog: false
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    // 去目录
    toDiretory (row) {
      const { id, subjectName: name } = row
      // id name 是约定好的参数，将来在目录按这种约定取值
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    // 去标签
    toTag (row) {
      const { id, subjectName: name } = row
      // id name 是约定好的参数，将来在目录按这种约定取值
      this.$router.push({ path: 'tags', query: { id, name } })
    },
    // 获取学科列表
    async getSubjects () {
      const { data } = await subjectsList(this.reqParams)
      this.subjects = data.items
      // console.log(data.items)
      this.total = data.counts
    },
    changePage (newPage) {
      this.reqParams.page = newPage
      this.getSubjects()
    },
    // 尺码
    changeSize (newPage) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newPage
      this.getSubjects()
    },
    // 搜索
    search () {
      // 在此业务中 关键字是双向绑定的 如果其他页面有需要改动的参数在搜索前改即可
      this.reqParams.page = 1
      this.getSubjects()
    },
    // 清除
    clear () {
      this.reqParams.subjectName = null
      this.getSubjects()
    },
    // 删除学科
    delSubject (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await subjectsDel({ id })
        this.$message.success('删除成功')
        this.getSubjects()
      }).catch(error => {
        console.log(error)
      })
    },
    // 新增学科
    addSubjects () {
      this.showDialog = true
    },
    // 修改,打开开对话框
    editSubject (row) {
      this.showDialog = true
      // 数据是{} 就是添加 数据是{id，subjectName,...}就是修改
      this.currSubject = row
      console.log(this.currSubject)
      console.log(this.currSubject.isFrontDisplay)
    }
  }
}
</script>

<style scoped lang='scss'>
.container-subjects{
  padding: 10px 15px;
  .pager{
    margin-top:15px;
    margin-bottom:15px;
    float:right
  }
}
  .el-link{
    margin-right: 10px;
  }

</style>
